package com.moensun.akka.springboot.akka;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.util.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.FiniteDuration;

import java.util.concurrent.TimeUnit;

import static akka.pattern.Patterns.ask;
import static com.moensun.akka.springboot.akka.SpringExtension.SpringExtProvider;

/**
 * Created by Bane.Shi.
 * Copyright MoenSun
 * User: Bane.Shi
 * Date: 2017/3/8
 * Time: 16:02
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

	@Autowired
	ActorSystem actorSystem;

	@RequestMapping(value = "",method = RequestMethod.GET)
	public String test(){
		ActorRef counter = actorSystem.actorOf(SpringExtProvider.get(actorSystem).props("CountingActor"),"counter");
		counter.tell(new CountingActor.Count(),null);
		FiniteDuration duration = FiniteDuration.create(3, TimeUnit.SECONDS);
		Future<Object> result = ask(counter, new CountingActor.Get(),Timeout.durationToTimeout(duration));

		try {
			System.out.println("Got back " + Await.result(result, duration));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "success";
	}

}
